#encoding: utf-8

from wtforms import StringField, IntegerField, DateField
from wtforms.validators import Email, InputRequired, Length, EqualTo, Regexp, URL
from ..forms import BaseForm
from utils import amancache
from wtforms import ValidationError
from flask import g

# 登录表单
class LoginForm(BaseForm):
    useraccount = StringField(validators=[Length(0, 50, message='请输入正确格式登录帐号'), InputRequired(message='请输入登录帐号')])
    password = StringField(validators=[Length(6, 20, message='请输入正确格式的密码')])
    status = StringField()
    isDel = StringField()
    remember = IntegerField()

# 修改密码表单
class ResetpwdForm(BaseForm):
    oldpwd = StringField(validators=[Length(6, 20, message='请输入正确格式的原密码')])
    newpwd = StringField(validators=[Length(6, 20, message='请输入正确格式的新密码')])
    newpwd2 = StringField(validators=[EqualTo("newpwd", message="确认新密码和新密码不一致")])

# 修改邮箱表单
class ResetEmailForm(BaseForm):
    email = StringField(validators=[Email(message='请输入正确格式的邮箱！')])
    captcha = StringField(validators=[Length(min=6,max=6, message='请输入正确长度的验证码！')])

    def validate_captcha(self, field):
        captcha = field.data
        email = self.email.data
        captcha_cache = amancache.get(email)
        if not captcha_cache or captcha.lower() != captcha_cache.lower():
            raise ValidationError('邮箱验证码错误！')

    def validate_email(self, field):
        email = field.data
        user = g.cms_user
        if user.email == email:
            raise ValidationError('新邮箱不能和原邮箱重复！')

# 创建后台用户表单
class CreateCMSUserForm(BaseForm):
    username = StringField(validators=[Length(1, 50, message='请输入正确格式的用户名！')])
    useraccount = StringField(validators=[Length(1, 50, message='请输入正确格式的帐号！')])
    password = StringField(validators=[Length(6, 20, message='请输入正确格式的密码！')])
    email = StringField(validators=[Email(message='请输入正确格式的邮箱！')])
    mobile = StringField(validators=[Regexp(r'1[23456789]\d{9}', message='请输入正确格式的手机号码！')])

# 修改用户
class UpdateCMSUserForm(BaseForm):
    cmsuser_id = IntegerField(validators=[InputRequired(message='用户id没有传！')])
    username = StringField(validators=[Length(1, 50, message='请输入正确格式的用户名！')])
    email = StringField(validators=[Email(message='请输入正确格式的邮箱！')])
    mobile = StringField(validators=[Regexp(r'1[23456789]\d{9}', message='请输入正确格式的手机号码！')])

# feedback
# 添加feedback
class AddFeedbackForm(BaseForm):
    # 必填项
    feedback_date = DateField(validators=[InputRequired(message='请输入正确的反馈日期！')])
    provider = StringField(validators=[InputRequired(message='请输入正确的反馈人！')])
    issue_desc = StringField(validators=[InputRequired(message='请输入正确的问题描述！')])
    business = StringField(validators=[InputRequired(message='请输入正确的所属业务线！')])
    reciver = StringField(validators=[InputRequired(message='请输入正确的应答人！')])
    ishandled = StringField(validators=[InputRequired(message='请输入正确的解决状态！')])
    issue_type = StringField(validators=[InputRequired(message='请输入正确的问题类型！')])
    issue_lever = StringField(validators=[InputRequired(message='请输入正确的严重程度！')])
    isBug = IntegerField(validators=[InputRequired(message='请输入正确的是否是bug！')])

    # 非必填项
    reason = StringField()
    solution = StringField()
    resolver = StringField()
    remark = StringField()
    image_url = StringField()
    bug_url = StringField()
    influence = StringField()


# 修改feedback，继承AddFeedbackForm
class UpdateFeedbackForm(AddFeedbackForm):
    feedback_id = IntegerField(validators=[InputRequired(message='请输入反馈记录id！')])


# 创建用例分组
class CreateCaseGroupForm(BaseForm):
    groupname = StringField(validators=[Length(1, 50, message='请输入正确格式的分组名称！')])
    groupcode = StringField(validators=[Length(1, 20, message='请输入正确格式的分组编码！')])

# 修改用例分组
class UpdateCaseGroupForm(CreateCaseGroupForm):
    group_id = IntegerField(validators=[InputRequired(message='分组id没有传！')])

# 添加用例
class AddCaseForm(BaseForm):
    case_name = StringField(validators=[Length(1, 50, message='请输入正确格式的用例名称')])
    case_url = StringField(validators=[URL(message='请输入正确格式的请求URL')])
    case_param = StringField()
    expect_res = StringField(validators=[Length(1, 100, message='请输入正确格式的预期结果')])
    headers = StringField()
    remark = StringField()
    status = IntegerField()
    request_method = StringField(validators=[Length(1, 8, message='请输入正确格式的请求方法')])
    group = IntegerField(validators=[InputRequired(message='没有设置分组')])

# 修改用例
class UpdateCaseForm(AddCaseForm):
    case_id = IntegerField(validators=[InputRequired(message='没有传用例id！')])

# 调试用例
class DebugCaseForm(AddCaseForm):
    case_id = IntegerField()

# 创建用例计划
class CreateCasePlanForm(BaseForm):
    plan_name = StringField(validators=[Length(1, 50, message='请输入正确格式的计划名称！')])
    plan_group = IntegerField()
    case_run_way = IntegerField(validators=[InputRequired(message='执行方式格式设置不正确！')])
    delay_time = IntegerField(validators=[InputRequired(message='间隔时间格式设置不正确！')])
    case_run_way_01 = StringField(validators=[Length(1, 2, message='请设置正确格式的执行方式的单位！')])

# 修改用例计划
class UpdateCasePlanForm(CreateCasePlanForm):
    plan_id = IntegerField()